#include "Message.h"
#include "UART.h"

#if 1
volatile unsigned long  xdata MsgBuff[MSGBUFF_SIZE];
volatile unsigned char  Writew;
volatile unsigned char  Readw;//

/*
函数功能：消息初始化
输入参数：无
输出参数：无
用到的全局变量：无
*/
void MsgInit(void)
{
	Writew  = Readw = 0;
}

/*
函数功能：写消息缓冲区，
输入参数：
	msg:表示消息参数，用drv_packed(type,id,val,spec)宏来打包消息后再传给这个函数写缓冲区
输出参数：无
用到的全局变量：无
*/

#define WriteMsg_DET(rd,wr,len)   ((len)-((wr)>=(rd)?(wr)-(rd):(wr)+(len)-(rd)))
void WriteMsg(unsigned long msg)
{
	if(WriteMsg_DET(Readw,Writew,MSGBUFF_SIZE) >= 1)
	{
//		PrintString1("WriteMsg!\r\n");
//		TX1_write2buff(msg&0xff);
//		TX1_write2buff((msg>>8)&0xff);
//		TX1_write2buff((msg>>16)&0xff);
//		TX1_write2buff((msg>>24)&0xff);
		MsgBuff[Writew++]=msg;
		if(Writew>=MSGBUFF_SIZE)
			Writew=0;
	}
}

/*
void WriteMsg(unsigned  int msg)
{
	PrintString1("WriteMsg!\r\n");
	MsgBuff[Writew++]=msg;
	if(Writew>=MSGBUFF_SIZE)
		Writew=0;
}*/

/*
函数功能：读消息缓冲区，
输入参数：无
输出参数：返回当前的消息，返回后需要强制转换成t_msg类型的结构数据
用到的全局变量：无
*/
unsigned long ReadMsg(void)
{
  unsigned long msg=0;
	
	if(Writew!=Readw)
	{
		msg = MsgBuff[Readw++];
//		TX1_write2buff(msg&0xff);
//		TX1_write2buff((msg>>8)&0xff);
//		TX1_write2buff((msg>>16)&0xff);
//		TX1_write2buff((msg>>24)&0xff);
//		PrintString1("ReadMsg!\r\n");
	}
	if(Readw>=MSGBUFF_SIZE)
		Readw=0;
	
	return msg;
}

/*
函数功能：清除消息
输入参数：无
输出参数：无
用到的全局变量：无
*/
void ClearMsg(void)
{
	Writew=0;Readw=0;
}




#else




#endif
